package org.zzz.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.zzz.entity.Attendance;
import org.zzz.entity.AttendanceVo;
import org.zzz.entity.vo.AttendanceShowVo;

import java.util.Date;
import java.util.List;

/**
 * @Author ZZF
 * @Time 2021/04/19 15:12
 */
public interface AttendanceMapper {

    String sql = "select a.id,s.name as sname,c.name as courseName,time,status,t.name as teacherName " +
            "from attendance a,student s,teacher t,course c " +
            "where a.sid = s.id and a.cid = c.id and a.teacher_id = t.id ";
    /**
     * 添加考勤表
     * @param attendance
     * @return
     */
    @Insert("insert into attendance value(#{id},#{sid},#{cid},#{time},#{status},#{teacherId})")
    int insertAttendance(Attendance attendance);

    /**
     *
     * @param attendance
     * @return
     */
    @Insert("insert into attendance(id,sid,time,status,teacher_id) value(#{id},#{sid},#{time},#{status},#{teacherId})")
    int insertAttendanceByClassId(Attendance attendance);

    /**
     * 根据班级和课程id生成考勤表
     * @param attendance
     * @return
     */
    @Insert("insert into attendance(id,sid,cid,time,status,teacher_id) value(#{id},#{sid},#{cid},#{time},#{status},#{teacherId})")
    int insertAttendanceByClassIdAndCourseId(Attendance attendance);

    /**
     * 修改考勤表
     * @param attendance
     * @return
     */
    @Update("update attendance set id = #{id},sid = #{sid},cid = #{cid},time = #{time},status = #{status},teacher_id = #{teacherId} where id = #{id}")
    int updateAttendance(Attendance attendance);

    @Update("update attendance set status = #{status} where id = #{id}")
    int updateAttendanceStatusById(@Param("id") String id,@Param("status") String status);

    /**
     * 根据日期获取考勤表
     * @param date
     * @return
     */
    @Select(sql + " and time like '%${date}%'")
    List<AttendanceVo> listAttendanceByDate(@Param("date") String date);

    /**
     * 根据班级获取考勤表
     * @param classId
     * @return
     */
    @Select(sql + " and s.class_id = #{classId}")
    List<AttendanceVo> listAttendanceByClass(@Param("classId") String classId);

    /**
     * 根据学生姓名获取考勤表
     * @param
     * @return
     */
    @Select(sql + "and s.name = #{name}")
    List<AttendanceVo> listAttendanceBySName(@Param("name") String name);

    /**
     * 根据学号获取考勤表
     * @param sid
     * @return
     */
    @Select("select a.id,s.name as sname,a.cid ,time,status,t.name as teacherName " +
            "from attendance a,student s,teacher t " +
            "where a.sid = s.id and a.teacher_id = t.id and  a.sid = #{sid}")
    List<AttendanceShowVo> listAttendanceBySid(@Param("sid") String sid);

    /**
     * 根据学号获取考勤信息
     * @param id
     * @return
     */
    @Select(sql +" and a.id = #{id}")
    AttendanceVo getAttendanceById(@Param("id") String id);

    /**
     * 查询所有考勤信息(根据教师账号)
     * @param tid
     * @return
     */
    @Select("select a.id,s.name sname,a.cid, c.name class_name, a.sid,a.time,a.status from attendance a,student s,class c where a.sid = s.id and s.class_id = c.id and teacher_id = #{tid}")
    List<AttendanceShowVo> listAttendanceVo(@Param("tid") String tid);

    /**
     * 根据班级获取考勤信息
     * @param tid
     * @param classId
     * @return
     */
    @Select("select a.id,s.name sname,a.cid, c.name class_name, a.sid,a.time,a.status " +
            "from attendance a,student s,class c where a.sid = s.id and s.class_id = c.id " +
            "and teacher_id = #{tid} and c.id = #{classId}")
    List<AttendanceShowVo> listAttendanceVoByClassId(@Param("tid") String tid,@Param("classId") String classId);

    /**
     * 根据学生名字获取考勤信息
     * @param sname
     * @return
     */
    @Select("select a.id,s.name sname,a.cid, c.name class_name, a.sid,a.time,a.status " +
            "from attendance a,student s,class c where a.sid = s.id and s.class_id = c.id " +
            "and teacher_id = #{tid} and s.name = #{sname}")
    List<AttendanceShowVo> listAttendanceVoBySname(@Param("tid") String tid,@Param("sname") String sname);

    /**
     * 根据日期获取考勤信息
     * @param tid
     * @param date
     * @return
     */
    @Select("select a.id,s.name sname,a.cid, c.name class_name, a.sid,a.time,a.status " +
            "from attendance a,student s,class c where a.sid = s.id and s.class_id = c.id " +
            "and teacher_id = #{tid} and a.time = #{date}")
    List<AttendanceShowVo> listAttendanceVoByDate(@Param("tid") String tid,@Param("date") String date);
}
